package biz.datalk.industrialland.common.result;


import biz.datalk.commons.utils.json.JsonUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 封装统一返回值
 *
 * @author tarofang@163.com
 * @date 2019年03月19日
 */
@SuppressWarnings("unused")
public class UnifyResult {

    /** 结果状态码 */
    private String rsCode;
    /** 状态码对应信息 */
    private String rsCause;
    /** 结果体 */
    private Object data;

    // constructor
    //====================================================================================
    private UnifyResult() {
        super();
    }

    private UnifyResult(String rsCode) {
        super();
        this.rsCode = rsCode;
    }

    private UnifyResult(String rsCode, String rsCause) {
        this.rsCode = rsCode;
        this.rsCause = rsCause;
    }

    // getter and setter
    //====================================================================================
    public String getRsCode() {
        return rsCode;
    }

    public UnifyResult setRsCode(String rsCode) {
        this.rsCode = rsCode;
        return this;
    }

    public String getRsCause() {
        return rsCause;
    }

    public UnifyResult setRsCause(String rsCause) {
        this.rsCause = rsCause;
        return this;
    }

    public Object getData() {
        return data;
    }

    public UnifyResult setData(Object data) {
        this.data = data;
        return this;
    }

    // custom function
    //====================================================================================
    public static UnifyResult create(String rsCode, String rsCause) {
        return new UnifyResult(rsCode, rsCause);
    }

    public static UnifyResult success() {
        return create(UnifyResultCode.SUCCESS, "操作成功");
    }
    public static UnifyResult success(String rsCause) {
        return create(UnifyResultCode.SUCCESS, rsCause);
    }

    public static UnifyResult fail(String rsCause) {
        return create(UnifyResultCode.FAIL, rsCause);
    }

    public static UnifyResult fail(String rsCode, String rsCause) {
        return create(rsCode, rsCause);
    }

    public UnifyResult mapData() {
        this.data = new HashMap<String, Object>(16);
        return this;
    }

    @SuppressWarnings("unchecked")
    public UnifyResult put(String key, Object value) {
        if (this.data instanceof Map) {
            ((Map) this.data).put(key, value);
        } else {
            throw new IllegalArgumentException(String.format("data class must be Map， but this is [%s]", (this.data == null ? "null" : this.data.getClass().getName())));
        }
        return this;
    }

    @SuppressWarnings("unchecked")
    public UnifyResult putAll(Map<String, ?> aMap) {
        if (!(this.data instanceof Map)) {
            throw new IllegalArgumentException(String.format("data class must be Map， but this is [%s]", (this.data == null ? "null" : this.data.getClass().getName())));
            // return this;
        }
        ((Map) this.data).putAll(aMap);
        return this;
    }

    public Map<String, Object> asMap() {
        Map<String, Object> map = new HashMap<>(16);
        map.put("rsCode", getRsCode());
        map.put("rsCause", getRsCause());
        map.put("data", getData());
        return map;
    }

    public String asJson() {
        return JsonUtil.getJsonFromObject(this);
    }

    public static String failJson(String rsCode, String rsCause) {
        return create(rsCode, rsCause).asJson();
    }

    public static String failJson(String rsCause) {
        return fail(rsCause).asJson();
    }

}
